﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IMethodDeclarationExtensions.cs" company="StyleCop for ReSharper Development Team">
//   Copyright (c) StyleCop for ReSharper Development Team. All rights reserved.
// </copyright>
// <summary>
//   Defines the IMethodDeclarationExtensions type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace JetBrains.ReSharper.Psi.CSharp.Tree.Extensions
{
    using Impl;

    /// <summary>
    /// I method declaration extensions.
    /// </summary>
    public static class IMethodDeclarationExtensions
    {
        #region Public Methods

        /// <summary>
        /// Get return type.
        /// </summary>
        /// <param name="declaration">
        /// The declaration.
        /// </param>
        /// <returns>
        /// </returns>
        public static IType GetReturnType(this IMethodDeclaration declaration)
        {
            var methodDeclarationNode = declaration as IMethodDeclarationNode;

            if (methodDeclarationNode == null)
            {
                return null;
            }

            var typeUsage = methodDeclarationNode.TypeUsage;

            if (typeUsage == null)
            {
                return null;
            }

            return CSharpTypeFactory.CreateType(typeUsage);
        }

        /// <summary>
        /// Indicates whether this method is declared with the new keyword.
        /// </summary>
        /// <param name="declaration">
        /// The method to check.
        /// </param>
        /// <returns>
        /// <c>true</c> if the declaration is declared with new otherwise <c>false</c>
        /// </returns>
        public static bool IsNew(this IMethodDeclaration declaration)
        {
            return declaration != null && ModifiersUtil.GetNew(declaration as IModifiersListOwnerNode);
        }

        #endregion
    }
}